package com.luoran.system.config;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class QQMailUtils {

    // QQ邮箱的SMTP服务器地址和端口
    private static final String SMTP_HOST = "smtp.qq.com";
    private static final String SMTP_PORT = "587"; // QQ邮箱的SMTP端口（587用于TLS，465用于SSL）

    // 发送邮件
    public static void sendEmail(String fromEmail, String authCode, String toEmail,
                                 String subject, String messageBody, boolean isHtml) throws MessagingException {

        // 设置邮件服务器属性
        Properties properties = new Properties();
        properties.put("mail.smtp.host", SMTP_HOST);
        properties.put("mail.smtp.port", SMTP_PORT);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true"); // 开启TLS

        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, authCode); // 使用QQ邮箱授权码
            }
        });

        // 创建邮件消息
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // 设置邮件内容
        if (isHtml) {
            message.setContent(messageBody, "text/html; charset=utf-8");
        } else {
            message.setText(messageBody);
        }

        // 发送邮件
        Transport.send(message);
        System.out.println("Email sent successfully.");
    }

    // 发送带附件的邮件
    public static void sendEmailWithAttachment(String fromEmail, String authCode, String toEmail,
                                               String subject, String messageBody, boolean isHtml, String[] attachFiles) throws MessagingException {

        // 设置邮件服务器属性
        Properties properties = new Properties();
        properties.put("mail.smtp.host", SMTP_HOST);
        properties.put("mail.smtp.port", SMTP_PORT);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true"); // 开启TLS

        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, authCode); // 使用QQ邮箱授权码
            }
        });

        // 创建邮件消息
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // 创建邮件正文部分
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        if (isHtml) {
            messageBodyPart.setContent(messageBody, "text/html; charset=utf-8");
        } else {
            messageBodyPart.setText(messageBody);
        }

        // 创建多部分邮件
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // 添加附件
        if (attachFiles != null && attachFiles.length > 0) {
            for (String filePath : attachFiles) {
                MimeBodyPart attachPart = new MimeBodyPart();
                try {
                    attachPart.attachFile(filePath);
                    multipart.addBodyPart(attachPart);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new MessagingException("Error attaching file: " + filePath, e);
                }
            }
        }

        // 设置邮件内容
        message.setContent(multipart);

        // 发送邮件
        Transport.send(message);
        System.out.println("Email sent successfully with attachments.");
    }
}
